package cmd

import (
	"fmt"
	"os"
	//"text/template"

	"gfast-gen-plus/internal/app/gen/command"
	"github.com/spf13/cobra"
)

var genApiCmd = &cobra.Command{
	Use:   "api [name]",
	Short: "生成api代码",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		name := args[0]

		command.NewApi().MakeFile(name)

		//// 读取模板文件
		//tmpl, err := template.ParseFiles("internal/app/gen/resouce/template/template.tmpl")
		//if err != nil {
		//	fmt.Println("Error reading template file:", err)
		//	return
		//}
		//
		//// 准备要填充的数据
		//data := struct {
		//	Name string
		//}{
		//	Name: name,
		//}
		//
		//// 创建生成的文件
		//file, err := os.Create(fmt.Sprintf("%s_generated.go", name))
		//if err != nil {
		//	fmt.Println("Error creating file:", err)
		//	return
		//}
		//defer file.Close()
		//
		//// 将模板应用到数据上，并输出到文件
		//if err := tmpl.Execute(file, data); err != nil {
		//	fmt.Println("Error executing template:", err)
		//	return
		//}
		//
		//fmt.Printf("Generated %s_generated.go\n", name)
	},
}

var generateCmd = &cobra.Command{
	Use:   "gen [name]",
	Short: "生成代码",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		//name := args[0]

		//command.NewApi().MakeFile()

		//// 读取模板文件
		//tmpl, err := template.ParseFiles("internal/app/gen/resouce/template/template.tmpl")
		//if err != nil {
		//	fmt.Println("Error reading template file:", err)
		//	return
		//}
		//
		//// 准备要填充的数据
		//data := struct {
		//	Name string
		//}{
		//	Name: name,
		//}
		//
		//// 创建生成的文件
		//file, err := os.Create(fmt.Sprintf("%s_generated.go", name))
		//if err != nil {
		//	fmt.Println("Error creating file:", err)
		//	return
		//}
		//defer file.Close()
		//
		//// 将模板应用到数据上，并输出到文件
		//if err := tmpl.Execute(file, data); err != nil {
		//	fmt.Println("Error executing template:", err)
		//	return
		//}
		//
		//fmt.Printf("Generated %s_generated.go\n", name)
	},
}

type Gen struct {
}

func NewGen() *Gen {
	return &Gen{}
}

func (c *Gen) Run() {
	var rootCmd = &cobra.Command{Use: "gfg"}
	rootCmd.AddCommand(genApiCmd)
	rootCmd.AddCommand(generateCmd)

	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
